#ifdef a
#include"ListNodeHead_TWO.h"

//头节点的建立
ListNode* ListNodeHeadCreate() {
	ListNode* head = (ListNode*)malloc(sizeof(ListNode));
	if (!head) {
		assert(0);
		return NULL;
	}
	head->next = head;
	head->prev = head;
	return head;
}

//新节点的创建
ListNode* BuyDlistNode(TYPE1 val) {
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	if (!newnode) {
		assert(0);
		return NULL;
	}
	newnode->next = NULL;
	newnode->prev = NULL;
	newnode->val = val;
	return newnode;
}

//尾插
void DListNodePushBack(ListNode* head,TYPE1 val) {
	assert(head);
	ListNode* new_node = BuyDlistNode(val);
	ListNode* cur = head;
	while (cur->next != head) {
		cur = cur->next;
	}
	new_node->next = cur->next;
	cur->next->prev = new_node;
	new_node->prev = cur;
	cur->next = new_node;
	return;
}

//尾删
void DListNodePopBack(ListNode* head) {
	//查空中已经assert
	if (ListNodeIsEmpty(head))
		return;

	ListNode* cur = head->next;
	ListNode* prev = head;
	while (cur->next != head) {
		prev = cur;
		cur = cur->next;
	}
	//要删除的是cur
	head->prev = prev;
	prev->next = head;
	free(cur);
	cur = NULL;
	return;
}

//查空
int ListNodeIsEmpty(ListNode* head) {
	assert(head);
	return head == head->next;
}
//链表一次遍历
void ListNodePrint(ListNode* head) {
	assert(head);
	ListNode* cur = head->next;
	while (cur != head) {
		printf("%d-->", cur->val);
		cur = cur->next;
	}
	printf("null\n");
	return;
}

//头删
void DListNodePopHead(ListNode* head) {
	if (ListNodeIsEmpty(head))
		return;
	ListNode* del_node = head->next;
	del_node->next->prev = head;
	head->next = del_node->next;
	return;
}
//链表头插
void DListNodePushHead(ListNode* head, TYPE1 val) {
	assert(head);
	ListNode* new_node = BuyDlistNode(val);
	ListNode* cur = head;
	new_node->next = head->next;
	head->next->prev = new_node;
	head->next = new_node;
	new_node->prev = head;
	return;
}

//任意位置后插入
void DListNodePushPos(ListNode* pos, TYPE1 val) {
	assert(pos);
	ListNode* new_node = BuyDlistNode(val);
	pos->next->prev = new_node;
	new_node->next = pos->next;
	new_node->prev = pos;
	pos->next = new_node;
	return;
}

//任意位置后删除
void DListNodePopPos(ListNode* head, ListNode* pos) {
	if (pos->next == head) {
		printf("头节点无法删除!\n");
		return;
	}
	ListNode* del_node = pos->next;
	del_node->next->prev = pos;
	pos->next = del_node->next;
	free(del_node);
	del_node = NULL;
	return;
}

//查找(返回第一个)
ListNode* ListNodeFindVal(ListNode* head, TYPE1 val) {
	if (ListNodeIsEmpty(head))
		return NULL;
	ListNode* cur = head->next;
	while (cur != head) {
		if (cur->val == val)
			return cur;
		cur = cur->next;
	}
	return NULL;
}

//节点个数
void ListNodeSize(ListNode* head) {
	assert(head);
	int size = 0;
	ListNode* cur = head->next;
	while (cur != head) {
		cur = cur->next;
		size++;
	}
	printf("size = %d\n", size);
	return;
}

//销毁
void ListNodeDestort(ListNode** head) {
	assert(head);
	ListNode* del_node = *head;
	ListNode* cur = (*head)->next;
	while (cur != *head) {
		del_node = cur;
		cur = cur->next;
		free(del_node);
	}
	free(*head);
	*head = NULL;
	return;
}

//---------------------------------------------------------
void TestDListNode() {
	ListNode* head = ListNodeHeadCreate();
	//尾插测试
	DListNodePushBack(head, 1);
	DListNodePushBack(head, 2);
	ListNodePrint(head);
	ListNodeSize(head);
	//头插测试
	DListNodePushHead(head, 0);
	DListNodePushHead(head, -1);
	ListNodePrint(head);
	ListNodeSize(head);
	//尾删测试
	DListNodePopBack(head);
	//DListNodePopBack(head);
	//DListNodePopBack(head);
	//DListNodePopBack(head);
	ListNodePrint(head);
	ListNodeSize(head);
	//头删测试
	DListNodePopHead(head);
	//DListNodePopHead(head);
	//DListNodePopHead(head);
	ListNodePrint(head);
	ListNodeSize(head);
	//0 1
	//任意位置插入
	ListNode* pos = ListNodeFindVal(head, 0);
	if (pos) {
		DListNodePushPos(pos, 5);
		ListNodePrint(head);
		ListNodeSize(head);
	}
	else {
		printf("不存在该节点!\n");
	}
	//
	pos = ListNodeFindVal(head, 2);
	if (pos) {
		DListNodePushPos(pos, 6);
		ListNodePrint(head);
		ListNodeSize(head);
	}
	else {
		printf("不存在该节点!\n");
	}
	//0 5 1
	//任意位置删除
	pos = ListNodeFindVal(head, 5);
	if (pos) {
		DListNodePopPos(head,pos);
		ListNodePrint(head);
		ListNodeSize(head);
	}
	else {
		printf("不存在该节点!\n");
	}
	//销毁
	ListNodeDestort(&head);
}
#endif 